/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Modulo4;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import org.json.*;
import java.io.PrintWriter;
import Modelo.DAL.Asistencia_DAO;

import Acceso.Security;
import Modelo.Asistencia;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMessage;

/**
 *
 * @author Jose Leonardo
 */
public class consultarAsistenciaAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private static final String FAILURE = "failure";

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        String[] a = {"Administrador", "Director DSI",
            "Jefe Apoyo Administrativo", "Personal Administrativo"};
        if (!Security.validateSession(request, a)) {
            return mapping.findForward("errorPriv");
        }

        response.setContentType("text/json");
        ActionErrors errors = new ActionErrors();

        String ci = request.getParameter("ci");
        String fechaD = request.getParameter("fechaDesde");
        String fechaH = request.getParameter("fechaHasta");
        String sede = request.getParameter("sede");
        String voe = request.getParameter("voe");
        String condicion = request.getParameter("condicion");

        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
        String order = request.getParameter("order");
        String asc = request.getParameter("asc");

        boolean hayError = false;

        //Comprueba que el campo cedula solo tenga numeros
        if (ci != null && !ci.equals("")) {
            if (!Acceso.FormatUtility.validatePattern(ci, 2)) {
                errors.add("invalid_Cedula", new ActionMessage("errors.invalid_Cedula"));
                saveErrors(request, errors);
                hayError = true;
            } else {
                ci = voe + "-" + ci;
            }
        }

        if (hayError) {
            errors.add("No_hay_Resultados", new ActionMessage("errors.No_hay_Resultados"));
            saveErrors(request, errors);

            JSONObject data = new JSONObject();
            data.put("error", "No hay resultados");
            PrintWriter pw = response.getWriter();
            pw.write(data.toString());
            pw.flush();
            pw.close();
            return mapping.findForward(FAILURE);
        }

        Asistencia asistencia = new Asistencia();
        asistencia.setCi(ci);
        asistencia.setCondicion(condicion);
        asistencia.setNombreSede(sede);


        JSONObject data = Asistencia_DAO.consultarAsistencia(asistencia, fechaD, fechaH, limit, offset, order, asc);
        PrintWriter pw = response.getWriter();
        pw.write(data.toString());
        pw.flush();
        pw.close();

        return mapping.findForward(SUCCESS);
    }
}
